package com.sjz.myfrist;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/***
 * 对数据库表的增加和删除
 */
public class SQLiteHelperActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView et_name;
    private UserDBHelper mHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_helper);

        et_name = findViewById(R.id.sqlite_helper_et_name);
        findViewById(R.id.sqlite_helper_btn_add).setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
//        打开数据库帮助器的实例
        mHelper = UserDBHelper.getInstance(this);
//        打开数据库帮助器的读写连接
        mHelper.openWriteLink();
        mHelper.openReadLink();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mHelper.closeLink();
    }

    @Override
    public void onClick(View view) {
        String name = et_name.getText().toString();
        User user = null;
        switch (view.getId()){
            case R.id.sqlite_helper_btn_add:
//                以下声明一个用户信息对象，并填写它的各字段值
                user = new User(name);
                if(mHelper.insert(user)>0){
                    Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}